package com.fangcun.service.admin.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.entity.MessageData;
import com.fangcun.entity.User;
import com.fangcun.entity.UserVirtualWallet;
import com.fangcun.entity.UserWithdrawCashList;
import com.fangcun.mapper.MessageDataMapper;
import com.fangcun.mapper.UserMapper;
import com.fangcun.mapper.UserVirtualWalletMapper;
import com.fangcun.mapper.UserWithdrawCashListMapper;
import com.fangcun.service.admin.UserWithdrawService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class UserWithdrawServiceImpl implements UserWithdrawService{
	
	
	@Autowired
	private UserVirtualWalletMapper userVirtualWalletMapper;
	@Autowired
	private UserWithdrawCashListMapper userWithdrawCashListMapper;
	@Autowired
	private MessageDataMapper messageDataMapper;
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public PageInfo<UserWithdrawCashList> list(PageBean<UserWithdrawCashList> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<UserWithdrawCashList> list = userWithdrawCashListMapper.listForAdmin(request.getParam());
		if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
	//后台提现 修改 功能
	@Override
	public void update(UserWithdrawCashList request,Long id) throws ProException {
		UserVirtualWallet wallet = userVirtualWalletMapper.selectByUserId(request.getUserId());
		//验证
		if(!wallet.getSign().equals(PublicMethod.getMd5(wallet.getUserId()+""+wallet.getBanlanceMoney()+""+wallet.getFrozenMoney(), 1, true))) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "验证钱包失败，联系管理员。");
		}
		MessageData mess = new MessageData(); 

		if(request.getWithdrawStatus()==2){
			//提现成功后 将 冻结 减去提现
			wallet.setFrozenMoney(wallet.getFrozenMoney().subtract(request.getWithdrawFee()));
			wallet.setSign(PublicMethod.getMd5(wallet.getUserId()+""+wallet.getBanlanceMoney()+""+wallet.getFrozenMoney(), 1, true));
			request.setActionTime(new Date());
			//发送通知//成功后 发系统消息 
			mess.setSendUserId(0L);
			mess.setReceiveUserId(request.getUserId());
			mess.setType(1);
			mess.setTitle("提现提醒！");
			mess.setMessage("提现成功！");
		}else if(request.getWithdrawStatus()==3) {
			//不同意提现（修改 回来冻结 金额）
			wallet.setBanlanceMoney(wallet.getBanlanceMoney().add(request.getWithdrawFee()));
			wallet.setFrozenMoney(wallet.getFrozenMoney().subtract(request.getWithdrawFee()));
			wallet.setSign(PublicMethod.getMd5(wallet.getUserId()+""+wallet.getBanlanceMoney()+""+wallet.getFrozenMoney(), 1, true));
			//失败后发送系统消息
			mess.setSendUserId(0L);
			mess.setReceiveUserId(request.getUserId());
			mess.setType(1);
			mess.setTitle("提现提醒！");
			mess.setMessage("提现失败！");
		}
		//添加审核人后台登录的
		User user = userMapper.selectByPrimaryKey(id);
		request.setVerifyUser(user.getUserName());
		if(messageDataMapper.insertSelective(mess) <=0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(),"发送系统消息失败！");
		}
		request.setSentNoticeTime(new Date());
		//修改价格后 从新加密 修改进去
		if(userVirtualWalletMapper.updateByPrimaryKeySelective(wallet) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
		//修改 提现记录修改
		if(userWithdrawCashListMapper.updateByPrimaryKeySelective(request)<=0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	
	@Override
	public UserWithdrawCashList details(Long id) throws ProException {
		UserWithdrawCashList type = userWithdrawCashListMapper.selectByPrimaryKey(id);
		if(type == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return type;
	}
}
